package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.po.system.Log;
import com.woniuxy.entity.po.system.LogRetention;
import com.woniuxy.mapper.LogMapper;
import com.woniuxy.service.LogService;
import com.woniuxy.util.PageHelperUtil;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @BelongsProject: community
 * @BelongsPackage: com.woniuxy.service
 * @Author: Quan
 * @Date: 2025/3/3 14:32
 */
@Service
@AllArgsConstructor
public class LogServiceImpl implements LogService {
	private LogMapper logMapper;
	private PageHelperUtil pageHelperUtil;


	@Override
	public ResponseData<?> queryLogInfo(PageParam<Log> pageParam) {
		return ResponseData.ok(pageHelperUtil.getPageInfo(pageParam, logMapper::queryLogInfo));
	}

	@Override
	public List<String> queryUsername() {
		return logMapper.queryUsername();
	}

	@Override
	public void addLog(Log log) {
		logMapper.addLog(log);
	}

	@Override
	public void delLog(String cutOffTime) {
		logMapper.delLog(cutOffTime);
	}

	/**
	 * @Description: 重置日志的保留时间
	 * @Param: [retention]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/7
	 */
	@Override
	public ResponseData<?> setRetention(Integer retention) {
		// 查询是否有这个对象
		LogRetention logRetention = logMapper.querySimpleLog();
		if (logRetention == null) {
			// 说明没有这条数据，将这个用户插入到数据库中
			logRetention = new LogRetention();
			logRetention.setLogRetentionTime(retention);
			logRetention.setLogType("simple_log");
			// 将数据插入到数据库中
			logMapper.addLogRetention(logRetention);
		} else {
			logRetention.setLogType("simple_log");
			logRetention.setLogRetentionTime(retention);
			// 说明存在这条数据，将对应id的数据中的保留时间进行更新
			logMapper.updateLogRetentionTime(logRetention);
		}
		return ResponseData.ok();
	}

	@Override
	public ResponseData<?> getRetention() {
		return ResponseData.ok(logMapper.queryLogRetention("simple_log"));
	}
}
